package com.alibaba.pet.service.impl;


import com.alibaba.basic.exception.BusinessException;
import com.alibaba.basic.service.impl.BaseServiceImpl;
import com.alibaba.basic.util.BaiduAuditUtils;
import com.alibaba.basic.util.LoginContext;
import com.alibaba.org.domain.Employee;
import com.alibaba.org.mapper.EmployeeMapper;
import com.alibaba.pet.domain.Pet;
import com.alibaba.pet.domain.PetDetail;
import com.alibaba.pet.domain.PetOnlineAuditLog;
import com.alibaba.pet.mapper.PetDetailMapper;
import com.alibaba.pet.mapper.PetMapper;
import com.alibaba.pet.mapper.PetOnlineAuditLogMapper;
import com.alibaba.pet.service.IPetService;
import com.alibaba.user.domain.Logininfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Resource
    private PetMapper petMapper;
    @Resource
    private PetDetailMapper petDetailMapper;
    @Resource
    private EmployeeMapper employeeMapper;

    @Resource
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Override
    @Transactional
    public void add(Pet pet) {
        //不用处理dirPath - dirPath在类型表中
        //保存宠物信息
        super.add(pet);
        //保存宠物明细
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail!=null) {
            petDetail.setPetId(pet.getId());
            petDetailMapper.save(petDetail);
        }
    }


    @Override
    @Transactional
    public void update(Pet pet) {
        //不用处理dirPath - dirPath在类型表中
        //修改宠物信息
        super.update(pet);
        //修改宠物明细信息
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail!=null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.updatePetDetailByPetId(petDetail);
        }
    }

    @Override
    @Transactional
    public void del(Long id) {
        //删除宠物明细
        petDetailMapper.delPetDetailByPetId(id);
        //删除宠物信息
        super.del(id);
    }

    @Override
    @Transactional
    public void patchDelete(Long[] ids) {

        for (Long id : ids) {
            del(id);
        }

    }

    @Override
    public void onsale(Long[] ids, HttpServletRequest request) {
        //获取登录信息对象
        Logininfo logininfo = LoginContext.getLoginUser(request);
        //获取审核人
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        for (Long id : ids) {
            Pet pet = petMapper.loadById(id);
            //1.上架之前对文本和图片做审核
            Boolean textBoo = BaiduAuditUtils.TextCensor(pet.getName());
            Boolean imageBoo = BaiduAuditUtils.ImgCensor(pet.getResources());
            if(textBoo && imageBoo){
                //2.审核通过：修改状态 + 添加上架的审核成功日志
                pet.setOnsaletime(new Date());
                pet.setState(1);
                petMapper.update(pet);

                //添加审核日志
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(pet.getId());
                log.setState(1);//1审核通过  0待审核  -1驳回
                log.setAuditTime(new Date());
                log.setNote("审核成功");
                log.setAuditId(employee.getId());
                petOnlineAuditLogMapper.save(log);
            }else{
                //3.审核失败：添加上架的审核失败日志
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(pet.getId());
                log.setState(-1);//1审核通过  0待审核  -1驳回
                log.setAuditTime(new Date());
                log.setNote("审核失败，内容有非法数据");
                log.setAuditId(employee.getId());
                petOnlineAuditLogMapper.save(log);
                throw new BusinessException("审核失败，内容有非法数据");
            }
        }
    }

    @Override
    public void offsale(Long[] ids) {
        Date offsaletime = new Date();
        petMapper.offsale(offsaletime,ids);
    }

}
